package ui;

import bgg4j.BggException;
import core.Library;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import javax.swing.JFileChooser;
import javax.swing.ListSelectionModel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.JTableHeader;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Insets;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import java.awt.event.KeyEvent;
import java.awt.Point;

public class WareHouse extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private Library BGLib = null;
	
	private Properties m_props = null;
	
	private TestModel m_table = null;
	
	private ResultTestModel m_result_table = null;

	private JMenuBar jJMenuBar = null;

	private JMenu jMenu = null;

	private JMenuItem jMenuItemReload = null;

	private JMenuItem jMenuItemQuit = null;

	private JTabbedPane jTabbedPane = null;

	private JScrollPane jScrollPane = null;

	private JMenuItem jMenuItemRefresh = null;

	private JTable jTableGameList = null;

	private JMenu jMenu_options = null;

	private JMenuItem jMenuItemSettings = null;

	private JDialog jDialogSettings = null;  //  @jve:decl-index=0:visual-constraint="938,65"

	private JTabbedPane jTabbedPane1 = null;

	private JPanel jPanelProxy = null;

	private JCheckBox jCheckBoxUseProxy = null;

	private JLabel jLabelProxyName = null;

	private JLabel jLabelProxyPort = null;

	private JTextField jTextFieldServer = null;

	private JTextField jTextFieldPort = null;

	private JPanel jPanelUser = null;

	private JPanel jPanel = null;

	private JButton jButtonOK = null;

	private JButton jButtonCancel = null;

	private JLabel jLabelName = null;

	private JTextField jTextFieldUserID = null;
	
	private WareHouse me = null;

	private JPanel jPanel1 = null;

	private JLabel jLabelStatus = null;

	private JScrollPane jScrollPane1 = null;

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
			gridBagConstraints16.gridx = 2;
			gridBagConstraints16.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints16.gridy = 0;
			GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
			gridBagConstraints15.gridx = 3;
			gridBagConstraints15.fill = GridBagConstraints.BOTH;
			gridBagConstraints15.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints15.gridy = 0;
			GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
			gridBagConstraints14.fill = GridBagConstraints.BOTH;
			gridBagConstraints14.gridy = 1;
			gridBagConstraints14.weightx = 1.0;
			gridBagConstraints14.weighty = 1.0;
			gridBagConstraints14.gridwidth = 4;
			gridBagConstraints14.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints14.gridx = 0;
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints13.gridy = 0;
			gridBagConstraints13.weightx = 1.0;
			gridBagConstraints13.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints13.gridx = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints12.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("String for search");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel, gridBagConstraints12);
			jPanel2.add(getJTextFieldSearch(), gridBagConstraints13);
			jPanel2.add(getJScrollPane2(), gridBagConstraints14);
			jPanel2.add(getJButton(), gridBagConstraints15);
			jPanel2.add(getJCheckBox(), gridBagConstraints16);
		}
		return jPanel2;
	}
	/**
	 * This method initializes jTextFieldSearch	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldSearch() {
		if (jTextFieldSearch == null) {
			jTextFieldSearch = new JTextField();
		}
		return jTextFieldSearch;
	}
	/**
	 * This method initializes jScrollPane2	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane2() {
		if (jScrollPane2 == null) {
			jScrollPane2 = new JScrollPane();
			jScrollPane2.setViewportView(getJTableResultQuery());
		}
		return jScrollPane2;
	}
	/**
	 * This method initializes jTableResultQuery	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableResultQuery() {
		if (jTableResultQuery == null) {
			String[] columnNames = {"ID", "TITLE","YEAR"};
			m_result_table = new ResultTestModel(columnNames,0);
			jTableResultQuery = new JTable(m_result_table);
			jTableResultQuery.setCellSelectionEnabled(true);
			jTableResultQuery.setColumnSelectionAllowed(false);
			jTableResultQuery.setShowVerticalLines(false);
			
			DefaultTableColumnModel columnModel = (DefaultTableColumnModel)jTableResultQuery.getColumnModel();
	        columnModel.getColumn(0).setPreferredWidth(50);		//ID
	        columnModel.getColumn(1).setPreferredWidth(200);	//Title
	        columnModel.getColumn(2).setPreferredWidth(50);	//Year
	        
	        JTableHeader header = jTableResultQuery.getTableHeader();
	        header.setDefaultRenderer(new SortButtonRenderer());
	        header.addMouseListener(new HeaderMouseListener());
	        jTableResultQuery.setRowSelectionAllowed(true);
	        jTableResultQuery.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	        jTableResultQuery.addMouseListener(new java.awt.event.MouseAdapter() {
	        	public void mouseClicked(java.awt.event.MouseEvent e) {
	        		if (SwingUtilities.isRightMouseButton(e) && jTableResultQuery.getSelectedColumnCount() == 1 && e.getClickCount() == 1){
	        			GameContextMenu popup = new GameContextMenu(jTableResultQuery);
	        			popup.setGoogleWord(jTextFieldOptionalWord.getText());
	        			popup.show(e.getComponent(), e.getX() + 10, e.getY() + 15);
	        		}
	        	}
	        });
	        jTableResultQuery.setDefaultRenderer(Object.class, new TestRenderer());
		}
		return jTableResultQuery;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setPreferredSize(new Dimension(34, 20));
			jButton.setMnemonic(KeyEvent.VK_UNDEFINED);
			jButton.setText("search");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						BGLib.search(jTextFieldSearch.getText(), jCheckBox.isSelected());
						System.out.println("Result:"+BGLib.getResultSize());
						refreshResultTable();
						me.setStatus("Result : "+BGLib.getResultSize() + " games");
					} catch (BggException e1) {
						// TODO 自動生成された catch ブロック
						e1.printStackTrace();
						me.setStatus("Error:" + e1.getStatusCode() + "[" + e1.getMessage() + "]");
					}
				}
			});
		}
		return jButton;
	}
	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBox() {
		if (jCheckBox == null) {
			jCheckBox = new JCheckBox();
			jCheckBox.setText("Exact Only");
		}
		return jCheckBox;
	}
	/**
	 * This method initializes jPanelGoogle	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelGoogle() {
		if (jPanelGoogle == null) {
			GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
			gridBagConstraints18.insets = new Insets(5, 5, 5, 5);
			GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
			gridBagConstraints17.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints17.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints17.weightx = 1.0;
			jLabelGoogle = new JLabel();
			jLabelGoogle.setText("Optional word");
			jPanelGoogle = new JPanel();
			jPanelGoogle.setLayout(new GridBagLayout());
			jPanelGoogle.add(jLabelGoogle, gridBagConstraints18);
			jPanelGoogle.add(getJTextFieldOptionalWord(), gridBagConstraints17);
		}
		return jPanelGoogle;
	}
	/**
	 * This method initializes jTextFieldOptionalWord	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldOptionalWord() {
		if (jTextFieldOptionalWord == null) {
			jTextFieldOptionalWord = new JTextField();
		}
		return jTextFieldOptionalWord;
	}
	/**
	 * This method initializes jMenuItemExport	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemExport() {
		if (jMenuItemExport == null) {
			jMenuItemExport = new JMenuItem();
			jMenuItemExport.setText("Export");
			jMenuItemExport.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					DumpList();
				}
			});
		}
		return jMenuItemExport;
	}
	public static void main(String[] args) {
        try{
            UIManager.getInstalledLookAndFeels();
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            
    		File homedir = new File(Library.HOMEDIR);
    		if (!homedir.exists()){
    			homedir.mkdir();
    		}
    		File dir = new File(Library.HOMEDIR + "imagecache");
    		if (! dir.exists()){
    			dir.mkdir();
    		}
        }catch(Exception e) {
            e.printStackTrace();
        }
		new WareHouse().setVisible(true);
	}
	/**
	 * This is the default constructor
	 */
	public WareHouse() {
		super();
		me = this;
		initialize();
		m_props = new Properties();
		this.jMenuItemRefresh.doClick();
	}

	public Library getBGLib(){
		return BGLib;
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(850, 459);
		this.setLocation(new Point(200, 200));
		this.setContentPane(getJPanel1());
		this.setJMenuBar(getJJMenuBar());
		this.setTitle("My Board Game Warehouse (ver.1.2.3)");
		this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("nasika.jpg")));
		jDialogSettings = this.getJDialogSettings();
	}
	
	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.setPreferredSize(new Dimension(0, 20));
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getJMenu_options());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu();
			jMenu.setText("File");
			jMenu.add(getJMenuItem());
			jMenu.add(getJMenuItem2());
			jMenu.add(getJMenuItemExport());
			jMenu.add(getJMenuItem1());
		}
		return jMenu;
	}

	private void refreshResultTable(){
		while(m_result_table.getRowCount() != 0){
			m_result_table.removeRow(0);
		}
		for (int i=0; i<BGLib.getResultSize(); i++){
			m_result_table.addRow(BGLib.getResultRawData(i));
		}
	}
	
	private void refreshTable(){
		
		while(m_table.getRowCount() != 0){
			m_table.removeRow(0);
		}
		for (int i=0; i<BGLib.size(); i++){
			m_table.addRow(BGLib.getTableRawData(i));
		}

	}
	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem() {
		if (jMenuItemReload == null) {
			jMenuItemReload = new JMenuItem();
			jMenuItemReload.setText("Update from BGG");
			jMenuItemReload.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Update run = new Update(me);
					Thread th = new Thread(run);
					th.start();
				}
			});
		}
		return jMenuItemReload;
	}

	/**
	 * This method initializes jMenuItem1	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem1() {
		if (jMenuItemQuit == null) {
			jMenuItemQuit = new JMenuItem();
			jMenuItemQuit.setText("Quit");
			jMenuItemQuit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return jMenuItemQuit;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.addTab("List", null, getJScrollPane(), null);
			jTabbedPane.addTab("Image", null, getJScrollPane1(), null);
			jTabbedPane.addTab("Search", null, getJPanel2(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem2() {
		if (jMenuItemRefresh == null) {
			jMenuItemRefresh = new JMenuItem();
			jMenuItemRefresh.setText("Refresh");
			jMenuItemRefresh.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					eraseGame();
					loadProps();
					BGLib = null;
					BGLib = new Library(jTextFieldUserID.getText());
					BGLib.load();
					refreshTable();
					drawGame();
					me.setStatus(String.valueOf(BGLib.size()) + " games");
				}
			});
		}
		return jMenuItemRefresh;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTableGameList == null) {
			String[] columnNames = {"ID", "TITLE", "RATING", "RANK" , "AVERAGE", "DESIGNER","YEAR","MIN","MAX","TIME"};
			m_table = new TestModel(columnNames,0);
			jTableGameList = new JTable(m_table);
			jTableGameList.setCellSelectionEnabled(true);
			jTableGameList.setColumnSelectionAllowed(false);
			jTableGameList.setShowVerticalLines(false);
			
			DefaultTableColumnModel columnModel = (DefaultTableColumnModel)jTableGameList.getColumnModel();
	        columnModel.getColumn(0).setPreferredWidth(50);		//ID
	        columnModel.getColumn(1).setPreferredWidth(200);	//Title
	        columnModel.getColumn(2).setPreferredWidth(50);		//Rating
	        columnModel.getColumn(3).setPreferredWidth(50);		//Rank
	        columnModel.getColumn(4).setPreferredWidth(50);		//Ave.
	        columnModel.getColumn(5).setPreferredWidth(200);	//Designer
	        columnModel.getColumn(6).setPreferredWidth(50);	//Year
	        columnModel.getColumn(7).setPreferredWidth(50);	//Min
	        columnModel.getColumn(8).setPreferredWidth(50);	//Max
	        columnModel.getColumn(9).setPreferredWidth(50);	//Time
	        
	        JTableHeader header = jTableGameList.getTableHeader();
	        header.setDefaultRenderer(new SortButtonRenderer());
	        header.addMouseListener(new HeaderMouseListener());
	        jTableGameList.setRowSelectionAllowed(true);
	        jTableGameList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	        jTableGameList.addMouseListener(new java.awt.event.MouseAdapter() {   
	        	public void mousePressed(java.awt.event.MouseEvent e) {    
	        		if (SwingUtilities.isRightMouseButton(e) && jTableGameList.getSelectedColumnCount() == 1){
	        			GameContextMenu popup = new GameContextMenu(jTableGameList);
	        			popup.setGoogleWord(jTextFieldOptionalWord.getText());
	        			popup.show(e.getComponent(), e.getX() + 10, e.getY() + 15);
	        		}
	        	}   
	        });
	        jTableGameList.setDefaultRenderer(Object.class, new TestRenderer());
		}
		return jTableGameList;
	}
	
	/**
	 * This method initializes jMenu_options	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu_options() {
		if (jMenu_options == null) {
			jMenu_options = new JMenu();
			jMenu_options.setText("Options");
			jMenu_options.add(getJMenuItemSettings());
		}
		return jMenu_options;
	}

	/**
	 * This method initializes jMenuItemSettings	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItemSettings() {
		if (jMenuItemSettings == null) {
			jMenuItemSettings = new JMenuItem();
			jMenuItemSettings.setText("Settings");
			jMenuItemSettings.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jDialogSettings.setBounds(me.getX() + 50, me.getY() + 50, 200, 200);
					jDialogSettings.setVisible(true);
				}
			});
		}
		return jMenuItemSettings;
	}

	/**
	 * This method initializes jDialogSettings	
	 * 	
	 * @return javax.swing.JDialog	
	 */
	private JDialog getJDialogSettings() {
		if (jDialogSettings == null) {
			jDialogSettings = new JDialog(this);
			jDialogSettings.setSize(new Dimension(267, 210));
			jDialogSettings.setModal(true);
			jDialogSettings.setResizable(false);
			jDialogSettings.setContentPane(getJPanel());
			jDialogSettings.setTitle("Settings");
		}
		return jDialogSettings;
	}

	/**
	 * This method initializes jTabbedPane1	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane1() {
		if (jTabbedPane1 == null) {
			jTabbedPane1 = new JTabbedPane();
			jTabbedPane1.addTab("User", null, getJPanelUser(), null);
			jTabbedPane1.addTab("Proxy", null, getJPanelProxy(), null);
			jTabbedPane1.addTab("Google", null, getJPanelGoogle(), null);
		}
		return jTabbedPane1;
	}

	/**
	 * This method initializes jPanelProxy	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelProxy() {
		if (jPanelProxy == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.weightx = 1.0;
			gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.gridy = 2;
			jLabelProxyPort = new JLabel();
			jLabelProxyPort.setText("Port");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints3.anchor = GridBagConstraints.EAST;
			gridBagConstraints3.gridy = 1;
			jLabelProxyName = new JLabel();
			jLabelProxyName.setText("Server");
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 0;
			jPanelProxy = new JPanel();
			jPanelProxy.setLayout(new GridBagLayout());
			jPanelProxy.add(getJCheckBoxUseProxy(), gridBagConstraints2);
			jPanelProxy.add(jLabelProxyName, gridBagConstraints3);
			jPanelProxy.add(jLabelProxyPort, gridBagConstraints4);
			jPanelProxy.add(getJTextFieldServer(), gridBagConstraints5);
			jPanelProxy.add(getJTextFieldPort(), gridBagConstraints6);
		}
		return jPanelProxy;
	}

	/**
	 * This method initializes jCheckBoxUseProxy	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxUseProxy() {
		if (jCheckBoxUseProxy == null) {
			jCheckBoxUseProxy = new JCheckBox();
			jCheckBoxUseProxy.setText("Use Proxy");
		}
		return jCheckBoxUseProxy;
	}

	/**
	 * This method initializes jTextFieldServer	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldServer() {
		if (jTextFieldServer == null) {
			jTextFieldServer = new JTextField();
		}
		return jTextFieldServer;
	}

	/**
	 * This method initializes jTextFieldPort	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldPort() {
		if (jTextFieldPort == null) {
			jTextFieldPort = new JTextField();
		}
		return jTextFieldPort;
	}

	/**
	 * This method initializes jPanelUser	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelUser() {
		if (jPanelUser == null) {
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridy = 0;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.gridy = 0;
			jLabelName = new JLabel();
			jLabelName.setText("BGG User ID");
			jPanelUser = new JPanel();
			jPanelUser.setLayout(new GridBagLayout());
			jPanelUser.add(jLabelName, gridBagConstraints8);
			jPanelUser.add(getJTextFieldUserID(), gridBagConstraints9);
		}
		return jPanelUser;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.weightx = 1.0D;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.gridy = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints.weightx = 1.0D;
			gridBagConstraints.anchor = GridBagConstraints.EAST;
			gridBagConstraints.gridy = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.BOTH;
			gridBagConstraints7.gridy = 0;
			gridBagConstraints7.ipadx = 169;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.weighty = 1.0;
			gridBagConstraints7.gridwidth = 2;
			gridBagConstraints7.gridx = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJTabbedPane1(), gridBagConstraints7);
			jPanel.add(getJButtonOK(), gridBagConstraints);
			jPanel.add(getJButtonCancel(), gridBagConstraints1);
		}
		return jPanel;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						me.saveProps();
					} catch (FileNotFoundException e1) {
						// TODO 自動生成された catch ブロック
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO 自動生成された catch ブロック
						e1.printStackTrace();
					}
					jDialogSettings.setVisible(false);
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jDialogSettings.setVisible(false);
				}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jTextFieldUserID	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldUserID() {
		if (jTextFieldUserID == null) {
			jTextFieldUserID = new JTextField();
		}
		return jTextFieldUserID;
	}
	
	private void loadProps() {
		try {
			File props = new File(Library.HOMEDIR + "/warehouse.props");
			if (props.exists()){
				m_props.loadFromXML(new FileInputStream(Library.HOMEDIR + "/warehouse.props"));
			}else{
				props.createNewFile();
				jMenuItemSettings.doClick();
			}
		} catch (InvalidPropertiesFormatException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
		
		jTextFieldUserID.setText(m_props.getProperty("settings.userid"));
		jCheckBoxUseProxy.setSelected(Boolean.valueOf(m_props.getProperty("settings.useproxy")));
		jTextFieldServer.setText(m_props.getProperty("settings.proxyserver"));
		jTextFieldPort.setText(m_props.getProperty("settings.proxyport"));
		
		if ((Boolean.valueOf(m_props.getProperty("settings.useproxy")) == true)){
			System.setProperty("http.proxyHost", m_props.getProperty("settings.proxyserver"));
			System.setProperty("http.proxyPort", m_props.getProperty("settings.proxyport"));
		}
		
		jTextFieldOptionalWord.setText(m_props.getProperty("settings.googleword"));
	}

	private void saveProps() throws IOException{
		m_props.setProperty("settings.userid", jTextFieldUserID.getText());
		m_props.setProperty("settings.useproxy", String.valueOf(jCheckBoxUseProxy.isSelected()));
		m_props.setProperty("settings.proxyserver", jTextFieldServer.getText());
		m_props.setProperty("settings.proxyport", jTextFieldPort.getText());
		m_props.setProperty("settings.googleword", jTextFieldOptionalWord.getText());
		m_props.storeToXML(new FileOutputStream(Library.HOMEDIR + "/warehouse.props"), null);
		if (BGLib != null){
			BGLib.setUserID(jTextFieldUserID.getText());
		}
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints11.insets = new Insets(1, 1, 1, 1);
			gridBagConstraints11.gridy = 1;
			jLabelStatus = new JLabel();
			jLabelStatus.setText(" ");
			jLabelStatus.setPreferredSize(new Dimension(0, 18));
			jLabelStatus.setHorizontalAlignment(SwingConstants.LEFT);
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = GridBagConstraints.BOTH;
			gridBagConstraints10.weighty = 1.0;
			gridBagConstraints10.weightx = 1.0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJTabbedPane(), gridBagConstraints10);
			jPanel1.add(jLabelStatus, gridBagConstraints11);
		}
		return jPanel1;
	}
	
	public void setStatus(String msg){
		jLabelStatus.setText(" " + msg);
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.addComponentListener(new java.awt.event.ComponentAdapter() {
				public void componentShown(java.awt.event.ComponentEvent e) {
					drawGame();
				}
			});
		}
		return jScrollPane1;
	}
	
	private ImagePanel imagep = null;

	private JPanel jPanel2 = null;

	private JLabel jLabel = null;

	private JTextField jTextFieldSearch = null;

	private JScrollPane jScrollPane2 = null;

	private JTable jTableResultQuery = null;

	private JButton jButton = null;

	private JCheckBox jCheckBox = null;

	private JPanel jPanelGoogle = null;

	private JLabel jLabelGoogle = null;

	private JTextField jTextFieldOptionalWord = null;

	private JMenuItem jMenuItemExport = null;

	private void drawGame(){
		String[] imagelist = BGLib.getImageArray();
		if (imagep == null){
			imagep = new ImagePanel(imagelist);
			imagep.setPreferredSize(new Dimension(10,10));
			jScrollPane1.setViewportView(imagep);
		}
	}
	
	public void eraseGame(){
		if (imagep != null){
			imagep.removeAll();
			imagep = null;
		}
	}
	
	public void DumpList(){
		JFileChooser chooser = new JFileChooser();
		chooser.setEnabled(true);
		int ret = chooser.showSaveDialog(this);
		switch(ret){
		case JFileChooser.ERROR_OPTION:
			break;
		case JFileChooser.CANCEL_OPTION:
			break;
		case JFileChooser.APPROVE_OPTION:
			File csvfile = chooser.getSelectedFile();
			FileOutputStream fos;
				try {
					fos = new FileOutputStream(csvfile);
					BufferedOutputStream bos = new BufferedOutputStream(fos);
					//PrintStream out = new PrintStream( bos );
					OutputStreamWriter osw = new OutputStreamWriter(bos, "ISO-8859-1");
					for (int j=0; j<jTableGameList.getColumnCount(); j++){
						//out.print(jTableGameList.getModel().getColumnName(j) + ",");
						osw.write(jTableGameList.getModel().getColumnName(j));
						if (j <jTableGameList.getColumnCount() - 1){
							osw.write(',');
						}
					}
					//out.println();
					osw.write('\n');
					for (int i=0; i<jTableGameList.getRowCount(); i++){
						for (int j=0; j<jTableGameList.getColumnCount(); j++){
							//out.print(jTableGameList.getModel().getValueAt(i, j).toString().replace(",", " ") + ",");
							osw.write(jTableGameList.getModel().getValueAt(i, j).toString().replace(",", " "));
							if (j <jTableGameList.getColumnCount() - 1){
								osw.write(',');
							}
						}
						//out.println();
						osw.write('\n');
					}
					osw.flush();
					osw.close();
					bos.close();
					fos.close();
				} catch (FileNotFoundException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				} catch (IOException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
			break;
		}
	}

}  //  @jve:decl-index=0:visual-constraint="26,5"

class Update implements Runnable{
	
	private WareHouse m_owner = null;
	
	public Update(WareHouse owner){
		m_owner = owner;
	}
	
	public void run() {
		// TODO 自動生成されたメソッド・スタブ
		try {
			m_owner.setStatus(" Updating ....");
			m_owner.getBGLib().Update();
			m_owner.getBGLib().updateImage(m_owner);
			m_owner.eraseGame();
			m_owner.setStatus("Updating ... done.");
		} catch (BggException e1) {
			// TODO 自動生成された catch ブロック
			e1.printStackTrace();
			m_owner.setStatus("Error:" + e1.getStatusCode() + "[" + e1.getMessage() + "]");
		} catch (IOException e1) {
			// TODO 自動生成された catch ブロック
			e1.printStackTrace();
		}
	}
	
}


